package com.lin.utils;

import com.lin.thread.AsyncManager;
import com.lin.tool.HBaseConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;
import java.io.IOException;

/**
 * 确保应用退出时能关闭后台线程
 */
@Component
public class ShutdownManager
{
    private static final Logger logger = LoggerFactory.getLogger("sys-user");

    @PreDestroy
    public void destroy()
    {
        shutdownAsyncManager();
        closeHbaseConn();

    }

    /**
     * 停止异步执行任务
     */
    private void shutdownAsyncManager()
    {
        try
        {
            System.out.println("====关闭后台任务任务线程池====");
            AsyncManager.me().shutdown();
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
        }
    }

    private void closeHbaseConn(){
        try {
            System.out.println("====关闭hbase连接====");
            HBaseConnection.closeConn();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
